package com.sgq.vue.controller.system;


import com.sgq.vue.entity.system.Permission;
import com.sgq.vue.result.Result;
import com.sgq.vue.service.system.PermissionService;
import com.sgq.vue.vo.query.PermissionQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SGQ
 * @since 2023-10-30
 */
@RestController
@RequestMapping("/api/permission")
public class PermissionController {

        @Autowired
        private PermissionService permissionService;
        /**
         * 查询菜单列表
         * @return
         */
        @GetMapping("/list")
        public Result getMenuList(PermissionQueryVo permissionQueryVo){
            //查询菜单列表
            List<Permission> permissionList =
                    permissionService.findPermissionList(permissionQueryVo);
            //返回数据
            return Result.ok(permissionList);
        }
        /**
         * 查询上级菜单列表
         * @return
         */
        @GetMapping("/parent/list")
        public Result getParentList(){
            //查询上级菜单列表
            List<Permission> permissionList =
                    permissionService.findParentPermissionList();
            //返回数据
            return Result.ok(permissionList);
        }
        /**
         * 根据id查询菜单信息
         * @param id
         * @return
         */
        @GetMapping("/{id}")
        public Result getMenuById(@PathVariable Long id){
            return Result.ok(permissionService.getById(id));
        }
        /**
         * 添加菜单
         * @param permission
         * @return
         */
        @PostMapping("/add")
        public Result add(@RequestBody Permission permission){
            if(permissionService.saveAndUpdateAdminPermission(permission).getId() != null){
                return Result.ok().message("菜单添加成功");
            }
            return Result.error().message("菜单添加失败");
        }
        /**
         * 修改菜单
         * @param permission
         * @return
         */
        @PutMapping("/update")
        public Result update(@RequestBody Permission permission){
            if(!ObjectUtils.isEmpty(permissionService.updateAndGet(permission))){
                return Result.ok().message("菜单修改成功");
            }
            return Result.error().message("菜单修改失败");
        }
        /**
         * 删除菜单
         * @param id
         * @return
         */
        @DeleteMapping("/delete/{id}")
        public Result delete(@PathVariable Long id){
            if(!ObjectUtils.isEmpty(permissionService.deleteAndGet(id))){
                return Result.ok().message("菜单删除成功");
            }
            return Result.error().message("菜单删除失败");
        }
        /**
         * 检查菜单下是否有子菜单
         * @param id
         * @return
         */
        @GetMapping("/check/{id}")
        public Result check(@PathVariable("id") Long id){
            //判断菜单是否有子菜单
            if(permissionService.hasChildrenOfPermission(id)){
                return Result.exist().message("该菜单下有子菜单，无法删除");
            }
            return Result.ok();
        }


}

